import axios from 'axios'
import {Message, Loading} from 'element-ui'
import fa from "element-ui/src/locale/lang/fa";
//请求后端的网关
const ConfigBaseURL = 'http://localhost:8002/'  //默认路径，这里也可以使用env来判断环境
let loadingInstance = null //这里是loading
const token = window.localStorage.getItem('token')


//使用create方法创建axios实例
export const Service = axios.create({
  timeout: 10000, // 请求超时时间
  baseURL: ConfigBaseURL,
  method: 'post',
  headers: {
    'Content-Type': 'application/json;charset=UTF-8',
    'token': token
  }
})
// 添加请求拦截器
Service.interceptors.request.use(config => {
  // console.log('config =', config)
  // loadingInstance = Loading.service({
  //   lock: false,
  //   text: '拼命加载中 ...'
  // })

  // loadingInstance.close()
  return config
})
// 添加响应拦截器
Service.interceptors.response.use(response => {
    // loadingInstance.close()
    return response.data
  }
  , error => {
    console.log('TCL: error', error)

    if (error.response.status === 503) {
      Message({
        message: '服务出了点小问题',
        type: 'error',
        duration: 3 * 1000
      })
      return Promise.reject(error)
    }

    if (error.response.status === 400) {
      Message({
        message: '参数异常!',
        type: 'error',
        duration: 3 * 1000
      })
      return Promise.reject(error)
    }

    const msg = error.Message !== undefined ? error.Message : ''
    Message({
      message: '网络出了点小问题' + msg,
      type: 'error',
      duration: 3 * 1000
    })
    // loadingInstance.close()
    return Promise.reject(error)
  }
)
